<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onConfirm"
      @cancel="onCancel"
    />
  </div>
</template>

<script>
import { updateUserProfile } from "@/request/home";
import { Toast } from "vant";
import dayjs from "dayjs";

export default {
  props: {
    date: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1990, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(this.date)
    };
  },
  methods: {
    // onChange(p, v, i) {
    //   // console.log(p, v, i);
    // },
    async onConfirm(value) {
      const date = dayjs(value).format("YYYY-MM-DD");
      Toast.loading({
        message: "保存中...",
        forbidClick: true
      });
      try {
        await updateUserProfile({
          name: date
        });
        Toast.success({
          message: "保存成功"
        });
        this.$emit("onCancel");
      } catch (error) {
        if (error && error.response && error.response.status === 429) {
          Toast.fail({
            message: "昵称已存在"
          });
        }
      }
    },
    onCancel() {
      this.$emit("onCancel");
    }
  }
};
</script>

<style>
</style>